//
//  BankCardBin.m
//  TCProject
//
//  Created by admin on 16/2/1.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "BankCardBin.h"

@implementation BankCardBin
{
    NSArray *_banks;
}

- (instancetype)init
{
    if (self = [super init]) {
        NSData *data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"bankbin" withExtension:@"json"]];
        _banks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    }
    return self;
}

- (NSString *)getNameOfBank:(NSString *)charBin offset:(int)offset
{
    NSInteger bin = 0, index = 0;
    bin = [charBin integerValue];
    index = [self binarySearch:bin];
    if (index == -1 ||  index > _banks.count) {
        return @"没有记录的卡号";
    }
    return [NSString stringWithFormat:@"%@ %@", _banks[index][@"bankName"], _banks[index][@"cardType"]];
}

- (NSInteger)binarySearch:(NSInteger)des
{
    NSInteger low = 0;
    NSInteger high = _banks.count;
    while (low < high) {
        
        if (des == [_banks[low][@"bin"] intValue]) {
            return low;
        }
        
        low++;
    }
    return -1;
}
@end
